using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace SAE.Plugin.Demo
{
    public class ListAllContents : SPSAdminJob
    {
        protected override int OnRun(object parameter)
        {
            try
            {
                if (parameter.GetType() == typeof(SPSite))
                {
                    using (SPSite site=parameter as SPSite)
                    {
                        if (site.AllWebs.Count > 0)
                        {
                            this.TotalProgressMaximumStep = site.AllWebs.Count;

                            foreach (SPWeb currentWeb in site.AllWebs)
                            {
                                using (currentWeb)
                                {
                                    base.ReportTotalProgress(0, currentWeb.Url);

                                    this.CurrentProgressMaximumStep = currentWeb.Lists.Count;
                                    this.ResetCurrentProgress();

                                    foreach (SPList currentList in currentWeb.Lists)
                                    {
                                        try
                                        {
                                            this.ReportCurrentProgress(0, string.Format("List:{0}", currentList.Title));

                                            this.ReportCurrentProgress(0, string.Format("Base Type:{0},Base Template:{1},Owner:{2},Created:{3}", currentList.BaseType, currentList.BaseTemplate, currentList.Author, currentList.Created));

                                            this.ReportCurrentProgress(0, "Content Types:");

                                            foreach (SPContentType ct in currentList.ContentTypes)
                                            {
                                                this.ReportCurrentProgress(0, ct.Name);
                                            }

                                            this.ReportCurrentProgress(1, string.Empty);
                                        }
                                        catch (ApplicationException ex)
                                        {
                                            this.ReportCurrentProgress(1, ex.Message);
                                        }
                                    }

                                    base.ReportTotalProgress(1, "--------------------------------------------" + Environment.NewLine);
                                }
                            }
                        }
                    }
                }
            }
            catch (ApplicationException ex)
            {
                this.ReportTotalProgress(0, ex.Message);
            }

            return 1;
        }
    }
}
